package service

import (
	"errors"
	"fmt"
	"github.com/go-xorm/xorm"
	_ "github.com/go-sql-driver/mysql"
	"imooc.com/hello/model"
	"log"
)

var DbEngin *xorm.Engine

//当引入此包，就会初始化，就算是多次导入也只会初始化一次
func init()  {
	driverName := "mysql"
	dataSourceName := "root:qingdao2021@(192.168.33.10:3306)/db_chat?charset=utf8"
	err := errors.New("")
	//如果:=会认为DbEngin为局部变量
	DbEngin, err = xorm.NewEngine(driverName, dataSourceName)
	if err != nil && err.Error() != "" {
		log.Fatal(err.Error())
	}

	//是否显示sql语句
	DbEngin.ShowSQL(true)
	//数据库最大的连接数(关系着数据库的性能)
	DbEngin.SetMaxOpenConns(2)

	//根据User的struct属性，自动创建表接口
	DbEngin.Sync2(new(model.User),
		new(model.Concat), new(model.Community), new(model.Message))

	fmt.Println("init mysql connected success !")
}